/**
 * 
 */
package edu.cmu.mism.dgjava.views;

import java.io.IOException;
import java.util.Properties;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import edu.cmu.mism.dgjava.data.ui.CommonParametersTypeBook;
import edu.cmu.mism.dgjava.data.ui.composites.AbstractDataComposite;
import edu.cmu.mism.dgjava.utils.IDGConstants;
import edu.cmu.mism.dgjava.utils.SWTUtils;
import edu.cmu.mism.dgjava.utils.UIUtility;

/**
 * @author Ankit
 * 
 */
public class BondDataView extends ViewPart {

	public static final String ID = "DGJava.view.bondDataView"; //$NON-NLS-1$

	private CommonParametersTypeBook _typeBook;
	private Button _calBtn;
	private Button _showTreeButton;

	private static final String DEFAULT_PRINICPAL = "100";
	private static final String DEFAULT_BOND_LIFE = "4.1";
	private static final String DEFAULT_COUPON_RATE = "5.000";
	private static final String DEFAULT_QUOTED_BOND_PRICE = "99.8147";

	private Combo _couponFrequencyComb;

	private Text _principalTxt;
	private Text _bondLifeTxt;
	private Text _couponRateTxt;
	private Text _quotedBondPriceTxt;

	private Label _optionDataLabel;

	// private Button _impVolatilityBtn;
	private Button _quotedStrikeBtn;
	private Button _putRadio;
	private Button _callRadio;

	private Text _resultPriceTxt;
	private Text _dvPerBasisPointTxt;
	private Text _gammaTxt;
	private Text _vegaTxt;

	private TableViewer _tableViewer;

	/**
	 * Attaches listeners to all the widgets creates in the view.
	 */
	private void addListeners() {

	}

	private ModifyListener _validator = new ModifyListener() {

		@Override
		public void modifyText(ModifyEvent e) {
			validate();
		}
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
	 * .Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {

		Composite container = SWTUtils.createComposite(parent, 2, -1, 1,
				GridData.FILL_BOTH);
		// Container to hold the bond data
		Composite bondDataContainer = SWTUtils.createComposite(container, 2,
				-1, 1);
		// Container to hold the table viewer
		Composite tableContainer = SWTUtils
				.createComposite(container, 1, -1, 1);
		// Container to hold the result data
		Composite resultComposite = SWTUtils.createComposite(container, 2, -1,
				1);
		// Container to hold the result data
		Composite controlButtonsContainer = SWTUtils.createComposite(container,
				2, -1, 1);

		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		tableContainer.setLayoutData(gd);
		controlButtonsContainer.setLayoutData(gd);

		// Create the group to hold the bond data
		createBondDataGroup(bondDataContainer);

		// Create the table viewer to hold the values of time and rate
		createTableViewer(tableContainer);

		// Create the group to hold the pricing model data
		createPricingModelGroup(bondDataContainer);

		// Create the container for holding result data
		createResultData(resultComposite);

		// Create the buttons
		createControlButtons(controlButtonsContainer);

		// Add listeners to all the widgets created
		addListeners();

	}

	/**
	 * @param container
	 * 
	 *            Create the buttons required for calculation and generation of
	 *            trees
	 */
	private void createControlButtons(Composite container) {

		this._calBtn = SWTUtils.createPushButton(container,
				Messages.Calculate_button);
		this._calBtn.addSelectionListener(new SelectionListener() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see
			 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
			 * .swt.events.SelectionEvent)
			 */
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
			}
		});

		GridData btnGd = new GridData(SWT.CENTER, SWT.NONE, false, false);
		btnGd.horizontalAlignment = GridData.CENTER;
		btnGd.verticalAlignment = GridData.CENTER;
		this._calBtn.setLayoutData(btnGd);

		this._showTreeButton = SWTUtils.createPushButton(container,
				Messages.SHOW_TREE);
		this._showTreeButton.addSelectionListener(new SelectionListener() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see
			 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
			 * .swt.events.SelectionEvent)
			 */
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
			}
		});

		this._showTreeButton.setLayoutData(btnGd);

	}

	/**
	 * @param container
	 *            Creating the user interface to hold the values of the result
	 */
	private void createResultData(Composite container) {
		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		container.setLayoutData(gd);

		GridData hGd = new GridData(GridData.FILL_HORIZONTAL);

		SWTUtils.createLabel(container, Messages.BondDataView_priceLabel, 1);
		this._resultPriceTxt = SWTUtils.createTextBox(container, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);

		SWTUtils.createLabel(container,
				Messages.BondDataView_dvPerBasisPointLabel, 1);
		this._dvPerBasisPointTxt = SWTUtils.createTextBox(container, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);

		SWTUtils.createLabel(container, Messages.BondDataView_gammaLabel, 1);
		this._gammaTxt = SWTUtils.createTextBox(container, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);

		SWTUtils.createLabel(container, Messages.BondDataView_vegaLabel, 1);
		this._vegaTxt = SWTUtils.createTextBox(container, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);

	}

	/**
	 * @param bondDataContainer
	 * 
	 *            Created the UI to hold the pricing model to be used and its
	 *            associated parameters
	 */
	private void createPricingModelGroup(Composite bondDataContainer) {

		SWTUtils.createSeparator(bondDataContainer, 2);
		this._optionDataLabel = SWTUtils.createLabel(bondDataContainer,
				Messages.BondDataView_optionDataLabel, 2);

		GridData btnGd = new GridData(SWT.FILL, SWT.NONE, false, false);
		btnGd.horizontalSpan = 2;
		this._optionDataLabel.setLayoutData(btnGd);

		GridData hGd = new GridData(SWT.FILL, SWT.FILL, true, true);
		hGd.horizontalSpan = 2;

		Composite compositeContainer = SWTUtils.createComposite(
				bondDataContainer, 1, -1, 1);
		compositeContainer.setLayoutData(hGd);

		Properties typeDataCompProperties = new Properties();

		try {
			typeDataCompProperties.load(AbstractDataComposite.class
					.getResourceAsStream("bondData.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		_typeBook = new CommonParametersTypeBook(compositeContainer, "",
				typeDataCompProperties);

		createOptionDataParameterGroup(bondDataContainer);

	}

	/**
	 * @param parameterContainer
	 * 
	 *            Creating the group to hold the option data
	 */
	private void createOptionDataParameterGroup(Composite parameterContainer) {

		// this._impVolatilityBtn = SWTUtils.createCheckBox(parameterContainer,
		// Messages.OptionDataView_implyVolatility, 1);

		this._quotedStrikeBtn = SWTUtils.createCheckBox(parameterContainer,
				Messages.BondDataView_quotedPrice, 1);

		this._putRadio = SWTUtils.createRadioButton(parameterContainer,
				Messages.OptionDataView_radio_put);
		this._callRadio = SWTUtils.createRadioButton(parameterContainer,
				Messages.OptionDataView_radio_call);
	}

	/**
	 * @param tableContainer
	 * 
	 *            Table viewer to hold the time and rates
	 */
	private void createTableViewer(Composite tableContainer) {

		String[] columnNames = { "Time(Yrs)", "Rate(%)" };
		_tableViewer = UIUtility.createTableViewer(tableContainer, columnNames);

	}

	/**
	 * @param container
	 * 
	 *            Group to hold the bond data
	 */
	private void createBondDataGroup(Composite container) {

		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		container.setLayoutData(gd);

		GridData hGd = new GridData(GridData.FILL_HORIZONTAL);

		SWTUtils
				.createLabel(container, Messages.BondDataView_principalLabel, 1);
		this._principalTxt = SWTUtils.createTextBox(container, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);
		this._principalTxt.setText(DEFAULT_PRINICPAL);

		SWTUtils.createLabel(container, Messages.BondDataView_bondLifeLabel, 1);
		this._bondLifeTxt = SWTUtils.createTextBox(container, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);
		this._bondLifeTxt.setText(DEFAULT_BOND_LIFE);

		SWTUtils.createLabel(container, Messages.BondDataView_couponRateLabel,
				1);
		this._couponRateTxt = SWTUtils.createTextBox(container, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);
		this._couponRateTxt.setText(DEFAULT_COUPON_RATE);

		SWTUtils.createLabel(container,
				Messages.BondDataView_quotedBondPriceLabel, 1);
		this._quotedBondPriceTxt = SWTUtils.createTextBox(container, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);
		this._quotedBondPriceTxt.setText(DEFAULT_QUOTED_BOND_PRICE);

		SWTUtils.createLabel(container,
				Messages.BondDataView_couponFrequencyLabel, 1);
		this._couponFrequencyComb = SWTUtils.createCombo(container,
				IDGConstants.COUPON_FREQUENCY_TYPE, 1);
		this._couponFrequencyComb.select(0);
		this._couponFrequencyComb.setLayoutData(hGd);

	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	@Override
	public void setFocus() {
		// viewer.getControl().setFocus();
	}

	/**
	 * Validate User's Input
	 */
	public void validate() {
		// TODO
	}

}
